/**
 * @file LoggerModule.h
 * @author  
 * @brief 日志模块
 * @version 1.0
 * @date 2020-07-23
 * 
 * @copyright Copyright (c) 2020
 * 
 */

#pragma once

#include <map>
#include "data_struct/base/LogBase.h"
#include "modules/AssistModule/TextLog.h"

namespace behavior_controller
{
    class LogPub
    {
    public:
        LogPub(){};
        template <typename Logger, typename... Args>

        /**
         * @brief 设置log的文件名称、等级、内容等
         * 
         * @param name 名称
         * @param lvl 等级
         * @param args 内容
         */
        static void SetLog(std::string name, LogLevel lvl, Args... args)
        {
            if (logsets_.count(name) == 0)
            {
                logsets_[name] = std::make_shared<LogSet>();
            }
            std::shared_ptr<LogBase> log_ptr =
                std::make_shared<Logger>(name, lvl, args...);

            logsets_[name]->AddLog(log_ptr);
        }

        /**
         * @brief 根据log登记名称获取log内容
         * 
         * @param name 
         * @return const std::shared_ptr<LogSet>& 
         */
        static const std::shared_ptr<LogSet> &GetLogger(const std::string &name)
        {
            if (logsets_.count(name) == 0)
            {
                SetLog<StdoutLogger>(name, LogLevel::debug);
            }
            return logsets_[name];
        }

        template <typename... Args>
        static void Error(std::string &&name, Args &&... args)
        {
            GetLogger(name)->Error(args...);
        }

        template <typename... Args>
        static void Warn(std::string &&name, Args &&... args)
        {
            GetLogger(name)->Warn(args...);
        }

        template <typename... Args>
        static void Info(std::string &&name, Args &&... args)
        {
            GetLogger(name)->Info(args...);
        }

        template <typename... Args>
        static void Debug(std::string &&name, Args &&... args)
        {
            GetLogger(name)->Debug(args...);
        }

        /**
         * @brief 用于log打印时的接口调用
         * 
         */
        template <typename... Args>
        static void Log(LogLevel lvl, std::string &&name, Args &&... args)
        {
            switch (lvl)
            {
            case LogLevel::debug:
                Debug(name, args...);
                break;
            case LogLevel::info:
                Info(name, args...);
                break;
            case LogLevel::warn:
                Warn(name, args...);
                break;
            case LogLevel::error:
                Error(name, args...);
                break;
            }
        }

        /**
         * @brief log清空
         * 
         */
        static void FlushAll();
        //static void Clear();

    private:
        static std::map<std::string, std::shared_ptr<LogSet>> logsets_;
    };

} // namespace behavior_controller
